define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 100;

    function initData() {
        getChannelData(1, rows);
        getUserData(1, rows);
        getGroupData(1, rows);
    }

    $('#form-msg').validate({
        rules: {
            msgSendedTitle: {
                required: true,
                minlength: 2,
                maxlength: 40
            },
            beginTime: {
                required: true
            },
            msgSendedContent: {
                required: true,
                minlength: 2,
                maxlength: 255
            }
        },
        msgSendedTitle: {
            msgTitle: "请正确填写"
        },
        beginTime: {
            msgTitle: "请正确填写"
        },
        msgSendedContent: {
            msgTitle: "请正确填写"
        },
        success: function () {
            releaseMsg();
            return false;
        }
    });

    $(document).on('click', '.query-channel', function () {
        getChannelData(1, rows);
    });

    $(document).on('click', '.query-user', function () {
        getUserData(1, rows);
    });

    $(document).on('click', '.query-group', function () {
        getGroupData(1, rows);
    });

    $(document).on('click', '.check-all-channel', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-id"]').prop("checked", true);//全选
            $('#tab-channel tbody tr').addClass('tr-click');
        } else {
            $('input[name="channel-id"]').prop("checked", false);//取消全选
            $('#tab-channel tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-user', function () {
        if ($(this).is(":checked")) {
            $('input[name="user-id"]').prop("checked", true);//全选
            $('#tab-user tbody tr').addClass('tr-click');
        } else {
            $('input[name="user-id"]').prop("checked", false);//取消全选
            $('#tab-user tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-group', function () {
        if ($(this).is(":checked")) {
            $('input[name="group-id"]').prop("checked", true);//全选
            $('#tab-group tbody tr').addClass('tr-click');
        } else {
            $('input[name="group-id"]').prop("checked", false);//取消全选
            $('#tab-group tbody tr').removeClass('tr-click')
        }
    });

    function releaseMsg() {
        var userinputs = $('input[name="user-id"]');
        var uids = new Array();
        $.each(userinputs, function (i, item) {
            if ($(item).is(':checked')) {
                uids.push(parseInt($(item).val()));
            }
        });
        var channelinputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(channelinputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        var groupinputs = $('input[name="group-id"]');
        var groupIds = new Array();
        $.each(groupinputs, function (i, item) {
            if ($(item).is(':checked')) {
                groupIds.push(parseInt($(item).val()));
            }
        });
        if (uids.length == 0 && channelIds.length == 0 && groupIds == 0) {
            toastInfo('请选择发布对象');
            return;
        }
        var msgSendedTitle = $.trim($('#msgSendedTitle').val());
        var msgSendedContent = $.trim($('#msgSendedContent').val());
        var beginTime = $.trim($('#beginTime').val());
        var sendType = $('#sendType').val();
        var msgType = $('#msgType').val();
        var ids = $('#ids').val();
        var data = {
            msgSendedTitle: msgSendedTitle,
            msgSendedContent: msgSendedContent,
            beginTime: beginTime,
            sendType: sendType,
            msgType: msgType,
            ids: ids,
            uids: JSON.stringify(uids),
            channelIds: JSON.stringify(channelIds),
            groupIds: JSON.stringify(groupIds)
        };
        $('.addSendedMsg').prop('disabled', 'disabled');
        $.post('/messages/addSendedMsg', data, function (o) {
            switch (o.code) {
                case 0:
                    toastSuccess('发布成功');
                    setTimeout(function () {
                        window.location.href = '/messages';
                    }, 1500);
                    break;
                default:
                    toastError(o.msg);
                    $('.addSendedMsg').removeAttr('disabled');
            }
        });

    }

    function getChannelData(page, rows) {
        var channelName = $.trim($('#channelName').val());
        var data = {
            channelName: channelName,
            page: page,
            rows: rows,
            channelStatus: '0'
        };
        $.post('/channel/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-channel', {list: list});
                    $('#tab-channel tbody').empty();
                    $('#tab-channel tbody').html(html);
                    $('.channel-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getUserData(page, rows) {
        var accountName = $.trim($('#accountName').val());
        var data = {
            accountName: accountName,
            page: page,
            rows: rows,
            statusCode: '0'
        };
        $.post('/user/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-user', {list: list});
                    $('#tab-user tbody').empty();
                    $('#tab-user tbody').html(html);
                    $('.user-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getGroupData(page, rows) {
        var groupName = $.trim($('#groupName').val());
        var data = {
            groupName: groupName,
            page: page,
            rows: rows,
            groupStatus: '0'
        };
        $.post('/group/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-group', {list: list});
                    $('#tab-group tbody').empty();
                    $('#tab-group tbody').html(html);
                    $('.group-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.channel-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getChannelData(num, rows)
        }
    });

    $('.user-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getUserData(num, rows)
        }
    });

    $('.group-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getGroupData(num, rows)
        }
    });

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});